﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 解释器模式
{
    /// <summary>
    /// 1.解释器模式InterpreterPattern
    /// 2.优缺点分析和使用场景
    /// 3.web开发模板替换的解释器模式
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {

            //string student = "Kind";
            //Console.WriteLine(ToNumberCommon(student));
            {
                Context context = new Context("ObjectIsNotFound");
                AIInterpreter iInterpreter = new AIInterpreter();
                iInterpreter.Conversion(context);
                Console.WriteLine(context.Get());
            }

            {
                Context context = new Context("ObjectIsNotFound");
                List<BaseInterpreter> interpreterList = new List<BaseInterpreter>() {
                    new JnInterpreter(),
                    new AIInterpreter(),
                    new ZeroInterpreter()
                };
                foreach (var item in interpreterList)
                {
                    item.Conversion(context);
                }
                //iInterpreter.Conversion(context);
                Console.WriteLine(context.Get());
            }
            Console.Read();
        }

        /// <summary>
        /// a b c d e f g h i     j k l m n....
        /// 1 2 3 4 5 6 7 8 9     a b c d e....
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private static string ToNumberCommon(string text)
        {
            if (string.IsNullOrEmpty(text))
                return "";
            List<string> numberList = new List<string>();
            //o t---j n
            //j n---a e
            //a i---1 9
            //other--0

            foreach (var item in text.ToLower().ToArray())
            {
                switch (item)
                {
                    case 'a':
                    case 'j':
                        numberList.Add("1");
                        break;
                    case 'b':
                    case 'k':
                        numberList.Add("2");
                        break;
                    case 'c':
                    case 'l':
                        numberList.Add("3");
                        break;
                    case 'd':
                    case 'm':
                        numberList.Add("4");
                        break;
                    case 'e':
                    case 'n':
                        numberList.Add("5");
                        break;
                    case 'f':
                        numberList.Add("6");
                        break;
                    case 'g':
                        numberList.Add("7");
                        break;
                    case 'h':
                        numberList.Add("8");
                        break;
                    case 'i':
                        numberList.Add("9");
                        break;
                    default:
                        numberList.Add("0");
                        break;
                }
            }
            return string.Concat(numberList);//数组链接成字符串
        }
    }
}
